package com.xiaoyu.lanling.feature.friend.viewholder

import `in`.srain.cube.views.list.ViewHolderBase
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.xiaoyu.base.app.App
import com.xiaoyu.base.utils.extensions.getViewTag
import com.xiaoyu.base.utils.extensions.setOnClickDebounceListener
import com.xiaoyu.base.utils.extensions.setViewTag
import com.xiaoyu.lanling.R
import com.xiaoyu.lanling.feature.friend.model.FriendItem
import com.xiaoyu.lanling.media.image.ImageViewUtil
import com.xiaoyu.lanling.router.Router
import com.xiaoyu.lanling.view.UserAvatarDraweeView
import com.xiaoyu.lanling.view.text.UserNameTextView

open class FriendViewHolder : ViewHolderBase<FriendItem>() {

    companion object {
        val sOnItemClickListener = View.OnClickListener {
            val item = it.getViewTag<FriendItem>() ?: return@OnClickListener
            val activity = App.getInstance().topActivity ?: return@OnClickListener
            Router.instance.gotoUserActivity(activity, item.user)
        }
    }

    private var mAvatar: UserAvatarDraweeView? = null
    private var mName: UserNameTextView? = null
    private var mAge: TextView? = null
    private var mDesc: TextView? = null

    override fun createView(layoutInflater: LayoutInflater, parent: ViewGroup): View? {
        val view = layoutInflater.inflate(getLayoutRes(), parent, false)
        mAvatar = view.findViewById(R.id.avatar)
        mName = view.findViewById(R.id.name)
        mAge = view.findViewById(R.id.age)
        mDesc = view.findViewById(R.id.desc)

        view.setOnClickDebounceListener(sOnItemClickListener)

        return view
    }

    open fun getLayoutRes(): Int {
        return R.layout.item_friend
    }

    override fun showData(position: Int, itemData: FriendItem) {
        ImageViewUtil.loadImage(mAvatar, itemData.user, 56)
        mName?.setUser(itemData.user)
        mAge?.text = itemData.age.toString()
        mAge?.setCompoundDrawablesRelativeWithIntrinsicBounds(if (itemData.user.isMale) R.drawable.icon_friend_list_male else R.drawable.icon_friend_list_female, 0, 0, 0)
        mAge?.setBackgroundResource(if (itemData.user.isMale) R.drawable.shape_friend_sex_male else R.drawable.shape_friend_sex_female)
        mDesc?.text = itemData.desc
        mCurrentView?.setViewTag(itemData)
    }
}